package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.*;
import com.baomidou.ant.service.*;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.ant.vo.request.*;
import com.baomidou.ant.vo.response.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/report-order")
public class ReportOrderController {
    @Autowired
    private IReportOrderService reportOrderService;
    @Autowired
    private IWorkOrderService workOrderService;
    @Autowired
    private IProcessInfoService processInfoService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IParentOrderService parentOrderService;

    //上报接口
    @PostMapping("/report")
    public Result report(@RequestBody RequestReportOrderVo requestReportOrderVo){
        WorkOrder workOrder=workOrderService.queryByOrderNum(requestReportOrderVo.getOrderNum());
        int[] ids=MyStringUtil.toInt(workOrder.getProcess());
        ProcessInfo processInfo=processInfoService.getById(workOrder.getNowProcess());
        if(workOrder.getNowProcess()==ids[0]&&ObjectUtils.isEmpty(requestReportOrderVo.getCoreNum())){
            return Result.getFailure().setMsg("该工序为第一道工序！</br>请填写该工序的CORE批号");
        }
        if(processInfo.isConsume()&&ObjectUtils.isEmpty(requestReportOrderVo.getStuffNum())){
            return Result.getFailure().setMsg("该工序为耗材工序！</br>请填写该工序的材料批号/编号");
        }
        if(!reportOrderService.checkAmount(requestReportOrderVo)){
            return Result.getFailure().setMsg("上报的总数量超出投入或结算量,请确认");
        }
        if(!reportOrderService.checkSupply(requestReportOrderVo)){
            return Result.getFailure().setMsg("非终检站不允许使用余数录入,请确认");
        }
//        if(!reportOrderService.checkWorker(requestReportOrderVo)){
//            return Result.getFailure().setMsg("上报失败，所选执行人必须是该工序的委派人员");
//        }
        if(reportOrderService.reportOrder(requestReportOrderVo)){
            return Result.getSuccess().setMsg("上报成功");
        }else
            return Result.getFailure().setMsg("上传失败,数量有误");
    }

    //审核界面打开
    @GetMapping("/getReportById/{id}")
    public Result getReportById(@PathVariable Integer id){
        WorkOrder workOrder=workOrderService.getById(id);
        QueryWrapper<ReportOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_num",workOrder.getOrderNum());
        List<ReportOrder> reportOrderList=reportOrderService.list(queryWrapper);
        List<ResponseListReportVo> result=reportOrderService.getReport(reportOrderList);
        return Result.getSuccess().setData(result);
    }


    //上报记录查看接口
    @PostMapping("/listReport")
    public Result listReport(@RequestBody RequestListReportVo requestListReportVo){
        QueryWrapper<ReportOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_num",requestListReportVo.getOrderNum());
        List<ReportOrder> reportOrderList=reportOrderService.list(queryWrapper);
        List<ResponseListReportVo> result=reportOrderService.getReport(reportOrderList);
        if(reportOrderList==null||reportOrderList.size()==0){
            return Result.getFailure().setMsg("未查询到上报记录，请确认");
        }
        return Result.getSuccess().setData(result);
    }

    //总数返回接口,表格数据赋予
    @GetMapping("/listNum/{orderNum}")
    public Result listNum(@PathVariable String orderNum){
        ResponseListNumVo responseListNumVo=reportOrderService.getNum(orderNum);
        return Result.getSuccess().setData(responseListNumVo);
    }

    //多次上报，总数返回
    @PostMapping("/listNumOfReported")
    public Result listNumOfReported(@RequestBody RequestReportedNumVo requestReportedNumVo){
        ResponseListNumOfReportedVo vo=reportOrderService.getNumOfReported(requestReportedNumVo);
        return Result.getSuccess().setData(vo);
    }

    //我的已办
    @PostMapping("/myDone")
    public Result myDone(@RequestBody RequestGetMyDoneVo requestGetMyDoneVo){
        if(ObjectUtils.isEmpty(requestGetMyDoneVo.getWorkerNo())){
            return Result.getFailure().setMsg("用户信息获取异常!请重新登陆！");
        }
        IPage<ResponseGetMyDoneVo> page=reportOrderService.getMyDone(requestGetMyDoneVo);
        return Result.getSuccess().setData(page);
    }

    //已办详情
    @GetMapping("/myDoneInfo/{reportId}")
    public Result myDoneInfo(@PathVariable Integer reportId){
        ReportOrder reportOrder=reportOrderService.getById(reportId);
        if(ObjectUtils.isEmpty(workOrderService.queryByOrderNum(reportOrder.getOrderNum()))){
            return Result.getFailure().setMsg("相关工单已被删除，无法查询!");
        }
        ResponseMyDoneInfoVo responseMyDoneInfoVo=reportOrderService.myDoneInfo(reportId);
        return Result.getSuccess().setData(responseMyDoneInfoVo);
    }

    //总数统计
    @PostMapping("/getTotal")
    public Result getTotal(@RequestBody RequestDtoVo requestDtoVo){
        int totalSize=reportOrderService.totalSize(requestDtoVo);
        return Result.getSuccess().setData(totalSize);
    }


    //实际执行人查询
    @GetMapping("/actualWorker/{orderNum}")
    public Result actualWorker(@PathVariable String orderNum){
        WorkOrder workOrder=workOrderService.queryByOrderNum(orderNum);
        int[] processIds =MyStringUtil.toInt(workOrder.getProcess());
        List<String> actualWorkerList=new ArrayList<>();
        for(int i=0;i<processIds.length;i++){
            QueryWrapper<ReportOrder> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("order_num",orderNum).eq("report_process",processIds[i]);
            List<ReportOrder> orderList=reportOrderService.list(queryWrapper);
            String name="";
            for(ReportOrder reportOrder:orderList){
                name=MyStringUtil.strToString(reportOrder.getProcessWorker(),name);
            }
            if(Objects.equals(name, "")){
                actualWorkerList.add("暂无报工数据");
            }else
                actualWorkerList.add(name);
        }
        return Result.getSuccess().setData(actualWorkerList);
    }

    //耗材查询
    @PostMapping("/checkOfStuff")
    public Result checkOfStuff(@RequestBody RequestCheckOfStuffVo vo){
        IPage<ResponseCheckOfStuffVo> iPage=reportOrderService.checkStuff(vo);
        return Result.getSuccess().setData(iPage);
    }

    //机台查询
    @PostMapping("/checkOfMachine")
    public Result checkOfMachine(@RequestBody RequestCheckOfMachineVo vo){
        IPage<ResponseCheckOfMachineVo> iPage=reportOrderService.checkMachine(vo);
        return Result.getSuccess().setData(iPage);
    }

    //报工校正信息获取
    @PostMapping("/getDeleteInfo")
    public Result getDeleteInfo(@RequestBody RequestGetDeleteInfoVo vo){
        WorkOrder workOrder=workOrderService.getById(vo.getOrderId());
        SysUser user=userService.getById(vo.getUserId());
        QueryWrapper<ReportOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_num",workOrder.getOrderNum());
        queryWrapper.orderByAsc("create_time");
        List<ReportOrder> reportOrderList=reportOrderService.list(queryWrapper);
        if((!workOrder.isCheck())||reportOrderList.size()==0){
            return Result.getFailure().setMsg("没有上报记录！");
        }else {
            ReportOrder reportOrder=reportOrderList.get(reportOrderList.size()-1);
            ResponseGetDeleteInfoVo responseGetDeleteInfoVo=new ResponseGetDeleteInfoVo();
            responseGetDeleteInfoVo.setProcessName(processInfoService.getById(reportOrder.getReportProcess()).getProcessName());
            responseGetDeleteInfoVo.setUserName(user.getUserName());
            responseGetDeleteInfoVo.setNowProcessId(reportOrder.getReportProcess());
            return Result.getSuccess().setData(responseGetDeleteInfoVo);
        }
    }
    //删除接口
    @PostMapping("/deleteInfo")
    public Result deleteInfo(@RequestBody RequestDeleteReportInfoVo vo){
        reportOrderService.deleteReport(vo);
        return Result.getSuccess().setMsg("删除成功");
    }

    @GetMapping("/getSome/{name}")
    public Result getS(@PathVariable String name){
        ResponseTodayDataVo vo=reportOrderService.getTodayData(name);
        return Result.getSuccess().setData(vo);
    }
}
